#include "BlockingQueue.hpp"
#include "Task.hpp"
#include <unistd.h>
#include <ctime>

const std::string ops = "+-*/%";

void* customer(void* args)
{
    BlockingQueue<Task>* bq = static_cast<BlockingQueue<Task>*>(args);
    while (1)
    {
        Task task = bq->custom();
        int src = task();

        int left, right;
        char op;
        task.get(&left, &right, &op);

        std::cout << " customer [" << pthread_self() << "] " << (unsigned long) time(nullptr) << "消费任务：" 
             << left << " " << op << " " << right << " = " << src << std::endl;
    }
}

void* producer(void* args)
{
    BlockingQueue<Task>* bp = static_cast<BlockingQueue<Task>*>(args);
    while (1)
    {
        // 制作数据
        int left = rand() % 50;
        int right = rand() % 20;
        char op = ops[rand() % 5];
        Task task(left, right, op);
        
        // 生产
        bp->produce(task);
        std::cout << "-----------------------------------------------------" << std::endl;
        std::cout << " producer [" << pthread_self() << "] " << (unsigned long) time(nullptr) << "生产任务：" 
             << left << " " << op << " " << right << "=?" << std::endl;
        sleep(2);
    }
}

int main()
{
    srand((unsigned long) time(nullptr) ^ getpid());
    // BlockingQueue<int> bq;
    BlockingQueue<Task> bq;
    pthread_t csmer, pdcer;
    pthread_create(&csmer, nullptr, customer, (void*) &bq);
    pthread_create(&pdcer, nullptr, producer, (void*) &bq);

    pthread_join(csmer, nullptr);
    pthread_join(pdcer, nullptr);
    return 0;
}